package com.emit.config;

//import com.emit.security.AuthenticationProviderImpl;

import com.emit.security.resultReturned.CustomizeAuthenticationFailureHandler;
import com.emit.security.resultReturned.CustomizeAuthenticationSuccessHandler;
import com.emit.security.resultReturned.CustomizeLogoutSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;


/**
 * @author wanglongquan
 * @date 2019-02-11
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
//    @Autowired
//    private AuthenticationProviderImpl authenticationProvider;
    @Autowired
    CustomizeAuthenticationSuccessHandler authenticationSuccessHandler;
    @Autowired
    CustomizeAuthenticationFailureHandler authenticationFailureHandler;
    @Autowired
    CustomizeLogoutSuccessHandler logoutSuccessHandler;

    /**
     * 说明：通过前端传进来的值判断用户是否存在，密码是否正确
     *
//     * @param auth
     * @return
     * @throws Exception
     * @Title: configure
     * @See: {@link #configure}
     * @author
     * @date 2021/03/22 11:34
     */
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) {
//        auth.authenticationProvider(authenticationProvider);
//    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/user/login").permitAll()
            .anyRequest()
            .authenticated()
            .and().formLogin().loginPage("http://172.19.235.101:8000/user/login").loginProcessingUrl("/user/login").permitAll()
            .successHandler(authenticationSuccessHandler)
            .failureHandler(authenticationFailureHandler).and()
            .logout()
            .permitAll()//允许所有用户
            //登出成功处理逻辑
            .logoutSuccessHandler(logoutSuccessHandler)
            .and().csrf().disable().cors()
            .and().sessionManagement().
            //同一账号同时登录最大用户数;
             maximumSessions(10);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        //返回值
        return super.authenticationManagerBean();
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "HEAD", "DELETE", "OPTION"));
        configuration.setAllowedHeaders(Arrays.asList("*"));
        configuration.addExposedHeader("Authorization");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

    @Bean
    public CorsFilter corsFilter() {
        return new CorsFilter(corsConfigurationSource());
    }

}